package com.foreveross.fetl.queue;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.foreveross.fetl.application.IAirChinaDomesticApplication;
import com.foreveross.fetl.queue.util.LogMessageUtils;
import com.foreveross.fetl.util.DateUtils;
import com.foreveross.fetl.util.StringUtils;
/**
 *  查源表CRAWL_PLANE_FREIGHT
 *  国航国内OTA报表模型预处理 
 *  各个舱位等级的最低价
 *  每个网站的每个航班保存为mysql表的一条记录
 * @author ljshi
 *
 */

@Component
public class AirChinaDomesticPreprocess {
	
	private static Logger log = Logger.getLogger("airChinaDomestic");
	
	@Inject IAirChinaDomesticApplication airChinaDomesticApplication;
	
	private int tasksNumber;//任务数 多少条rowkey
	
	private int failuresNumber;//失败数
	
	private int insertNumber; //插入条数 
	
	private long excuteStartTime;
	
	private String taskTime=null;
	
	public void  rollBackNumber(){
		tasksNumber=0;
		failuresNumber=0;
		insertNumber=0;
		excuteStartTime = System.currentTimeMillis();
		LogMessageUtils.create("国航国内OTA", this.getClass().getSimpleName());
	}
	public void execute(){
		final Calendar c = Calendar.getInstance();
		c.add(Calendar.HOUR_OF_DAY, -1);
		rollBackNumber();
		taskTime=DateUtils.format(c.getTime(), "yyyy-MM-dd HH");
		try {
			process(c.getTime(), new Date());
		} catch (Exception e) {
			LogMessageUtils.build("国航国内OTA", this.getClass().getSimpleName(), 3, StringUtils.getStackTrace(e), failuresNumber, tasksNumber, 
					insertNumber, taskTime, excuteStartTime);
		}
		LogMessageUtils.build("国航国内OTA", this.getClass().getSimpleName(), 2, null, 
				failuresNumber, tasksNumber, insertNumber, taskTime, excuteStartTime);
		
	}
	
	public void manualExecute(final String start,String time){
		this.rollBackNumber();
		try {
		List<String> timeList = Arrays.asList(time.split(";"));
		Calendar c = Calendar.getInstance();
		taskTime = start + " " +time;
		for (String s : timeList) {
			String str =start;
			str += s;
			Date startTime = DateUtils.parse(str, "yyyyMMddHH");
			c.setTime(startTime);
			c.add(Calendar.HOUR_OF_DAY, +1);
			Date endTime = c.getTime();
			this.process(startTime, endTime);
			}
		LogMessageUtils.build("国航国内OTA", this.getClass().getSimpleName(), 2, null, 
				failuresNumber, tasksNumber, insertNumber, taskTime, excuteStartTime);
		} catch (Exception e) {
			LogMessageUtils.build("国航国内OTA", this.getClass().getSimpleName(), 3, StringUtils.getStackTrace(e), failuresNumber, tasksNumber, 
					insertNumber, taskTime, excuteStartTime);
		}
	}
	

	private void process(Date start, Date end) {
		log.info("AirChinaDomesticPreprocess start:"
				+ DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
		List<String> listLine = new ArrayList<String>();
		Collections.addAll(listLine, "PEK-CAN", "PEK-CTU", "PEK-SZX","PEK-SHA", 
				"PEK-NNG", "PEK-HGH", "PEK-CKG", "PEK-URC",
				"PEK-KWE", "PEK-HRB", "PEK-DLC");
	
		List<String> airCompanys = this.getAirCompanys();
		for(String str :listLine){
			String[] lines=str.split("-");
			Calendar cFlightDate = Calendar.getInstance();
			cFlightDate.setTime(start);
			int count = cFlightDate.get(Calendar.DAY_OF_WEEK);
			tasksNumber++;
			try {
				if (count == 2) {
					insertNumber += airChinaDomesticApplication.creatReport(
							airCompanys,  lines[0], lines[1],
							start, cFlightDate.getTime());
					cFlightDate.add(Calendar.DAY_OF_MONTH, 4);
					insertNumber += airChinaDomesticApplication.creatReport(
							airCompanys, lines[0], lines[1],
							start, cFlightDate.getTime());
				} else {
					cFlightDate.add(Calendar.DAY_OF_MONTH, 1);
					insertNumber += airChinaDomesticApplication.creatReport(
							airCompanys, lines[0], lines[1],
							start, cFlightDate.getTime());
					cFlightDate.add(Calendar.DAY_OF_MONTH, 3);
					insertNumber += airChinaDomesticApplication.creatReport(
							airCompanys,  lines[0], lines[1],
							start, cFlightDate.getTime());

				}
			} catch (Exception e) {
				log.error(StringUtils.getStackTrace(e));
				failuresNumber++;
			}
		}
		
	}
	

	private List<String> getAirCompanys() {
		List<String> airCompanys = new ArrayList<String>();
		airCompanys.add("20000"); //深航
		airCompanys.add("20001"); //东航
		airCompanys.add("20002"); //山东
		airCompanys.add("20003"); //川航
		airCompanys.add("20004"); //海航
		airCompanys.add("20005"); //南航
		airCompanys.add("20006"); //国航
		airCompanys.add("20007"); //厦航
		airCompanys.add("20011"); //首都
		
		airCompanys.add("00003"); //携程
		airCompanys.add("00004"); //艺龙
		return airCompanys;
	}

}
